package tmts;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import tmts.helpers.Encryption;

import models.Client;
import models.Lap;
import models.Skater;
import net.sf.json.JSONObject;

public class ClientSendHandler
{
    static ArrayList<Client> clients = new ArrayList<Client>();

    public static void addClient(Client client)
    {
        clients.add(client);
        sendAcceptMessage(client);
    }

    public static void sendAcceptMessage(Client client)
    {
        for (Client c : clients)
        {
            if (c.equals(client))
            {
                try
                {
                    Socket socket = new Socket(client.ip, client.port);
                    ClientSendHandler.sendMessage("client accepted", socket);

                } catch (UnknownHostException e)
                {
                    e.printStackTrace();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void pushNotification(Skater skater, Lap lap)
    {
        Server.db_handler.insertLine(skater, lap);
        JSONObject message = new JSONObject();
        message.put("ID", skater.getSkaterId());
        message.put("name", skater.getSkaterName());
        message.put("country", skater.getCountry());
        message.put("time", lap.getTime());
        message.put("laptime", lap.getLaptime());
        message.put("distance", lap.getDistance());

        Server.debug(message.toString());

        Server.debug("Sending to " + clients.size() + " clients");

        for (Client c : clients)
        {

            Socket client;
            try
            {
                client = new Socket(c.ip, c.port);
                ClientSendHandler.sendMessage(message.toString(), client);
                client.close();
            } catch (UnknownHostException e)
            {
                e.printStackTrace();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void sendMessage(String data, Socket client)
    {
        try
        {
            Server.debug("sendMessage " + data);

            PrintWriter out = new PrintWriter(client.getOutputStream(), true);
            out.println(Encryption.encrypt(data));
        } catch (UnknownHostException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (InvalidKeyException e)
        {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        } catch (NoSuchPaddingException e)
        {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e)
        {
            e.printStackTrace();
        } catch (BadPaddingException e)
        {
            e.printStackTrace();
        }
    }

    public static boolean clientExists(Client client)
    {
        for (Client c : clients)
        {
            if (c.equals(client))
                return true;
        }
        return false;
    }
}
